package cn.e4j.modular.crm.service.impl;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.GwavT;
import cn.e4j.modular.crm.entity.GwavlT;
import cn.e4j.modular.crm.mapper.GwavTMapper;
import cn.e4j.modular.crm.service.GwavTService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  商机阶段 服务实现类
 * </p>
 *
 * @author mengyb
 * @since 2019-04-12
 */
@Service
public class GwavTServiceImpl extends ServiceImpl<GwavTMapper,GwavT> implements GwavTService {

    @Transactional(readOnly = true)
    @Override
    public List<Map<String, Object>> getList(Dto dto, Page<Object> page) {

        ShiroUser shiroUser = ShiroKit.getUser();

        dto.put("ent", shiroUser.getDBC());
        dto.put("lang", LangUtils.getLang(shiroUser.getLANG()));

        return this.baseMapper.getList(dto, page);

    }

    @Transactional
    @Override
    public void add(GwavT gwavT, GwavlT gwavlT) {

        ShiroUser shiroUser = ShiroKit.getUser();

        gwavT.setGWAVENT(shiroUser.getDBC());

        gwavT.setGWAV002(" ");
        gwavT.setGWAV003(" ");
        gwavT.setGWAVUSER(shiroUser.getACCOUNT());
        gwavT.setGWAVGRUP(shiroUser.getORG_ID());
        gwavT.setGWAVORIU(shiroUser.getACCOUNT());
        gwavT.setGWAVORIG(shiroUser.getORG_ID());
        gwavT.setGWAVDATE(LocalDate.now());
        gwavT.setGWAVTIME(DateUtils.formatDate(new Date(), "HH:mm:ss"));

        gwavlT.setGWAVLENT(shiroUser.getDBC());
        gwavlT.setGWAVL001(gwavT.getGWAV001());
        gwavlT.setGWAVL002(LangUtils.getLang(shiroUser.getLANG()));

        gwavT.insert();
        gwavlT.insert();
    }

    @Transactional
    @Override
    public void edit(GwavT gwavT, GwavlT gwavlT) {
        ShiroUser shiroUser = ShiroKit.getUser();
        gwavT.setGWAVMODU(shiroUser.getACCOUNT());

        UpdateWrapper<GwavT> gwavTUpdateWrapper = new UpdateWrapper<>();
        gwavTUpdateWrapper.eq("GWAVENT", gwavT.getGWAVENT());
        gwavTUpdateWrapper.eq("GWAV001", gwavT.getGWAV001());

        UpdateWrapper<GwavlT> gwavlTUpdateWrapper = new UpdateWrapper<>();
        gwavlTUpdateWrapper.eq("GWAVLENT", gwavT.getGWAVENT());
        gwavlTUpdateWrapper.eq("GWAVL001", gwavT.getGWAV001());

        gwavT.update(gwavTUpdateWrapper);
        gwavlT.update(gwavlTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRow(GwavT gwavT, GwavlT gwavlT) {

        UpdateWrapper<GwavT> gwavTUpdateWrapper = new UpdateWrapper<>();
        gwavTUpdateWrapper.eq("GWAVENT", gwavT.getGWAVENT());
        gwavTUpdateWrapper.eq("GWAV001", gwavT.getGWAV001());

        UpdateWrapper<GwavlT> gwavlTUpdateWrapper = new UpdateWrapper<>();
        gwavlTUpdateWrapper.eq("GWAVLENT", gwavT.getGWAVENT());
        gwavlTUpdateWrapper.eq("GWAVL001", gwavT.getGWAV001());


        gwavT.delete(gwavTUpdateWrapper);
        gwavlT.delete(gwavlTUpdateWrapper);

    }

    @Transactional
    @Override
    public void delRows(List<GwavT> gwavTList) {
        for (GwavT gwavT : gwavTList) {
            UpdateWrapper<GwavT> gwavTUpdateWrapper = new UpdateWrapper<>();
            gwavTUpdateWrapper.eq("GWAVENT", gwavT.getGWAVENT());
            gwavTUpdateWrapper.eq("GWAV001", gwavT.getGWAV001());

            GwavlT gwavlT=new GwavlT();
            UpdateWrapper<GwavlT> gwavlTUpdateWrapper = new UpdateWrapper<>();
            gwavlTUpdateWrapper.eq("GWAVLENT", gwavT.getGWAVENT());
            gwavlTUpdateWrapper.eq("GWAVL001", gwavT.getGWAV001());

            gwavT.delete(gwavTUpdateWrapper);
            gwavlT.delete(gwavlTUpdateWrapper);
        }
    }

    /**
     * 查重
     * @param gwavT
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Boolean existedTheParams(GwavT gwavT) {
        QueryWrapper<GwavT> gwavTQueryWrapper = new QueryWrapper<>();
        gwavTQueryWrapper.eq("GWAVENT", gwavT.getGWAVENT());
        gwavTQueryWrapper.eq("GWAV001", gwavT.getGWAV001());
        return this.baseMapper.selectList(gwavTQueryWrapper).size() > 0;
    }
}
